import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';


import '../../../common/cpn/cpn_view_state.dart';
import '../../../common/cpn/sliver_app_bar.dart';
import '../../../router/routers.dart';
import '../../../theme/color_palettes.dart';
import 'feedback_logic.dart';

class FeedbackPage extends CpnViewState<FeedbackLogic> {
  FeedbackPage({Key? key}) : super(key: key, bindViewState: false);
  final RxInt _selectedFeedbackType = 0.obs;
  final TextEditingController _feedbackController = TextEditingController();
  final RxBool _isAnonymous = false.obs;

  // 反馈类型选项
  final List<Map<String, dynamic>> feedbackTypes = [
    {'icon': Icons.bug_report, 'title': '问题反馈'},
    {'icon': Icons.lightbulb_outline, 'title': '功能建议'},
    {'icon': Icons.thumb_up, 'title': '好评鼓励'},
    {'icon': Icons.help_outline, 'title': '其他问题'},
  ];

  @override
  Widget buildBody(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[50],
      body: CustomScrollView(
        slivers: [
          commonSliverAppBar(
            title: '反馈问题',
            action: [
              Icon(Icons.history,
                  color: ColorPalettes.instance.firstText,  size: 36.w)
            ],
            actionClick: [
                  () {
                AppRoutes.jumpPage(AppRoutes.feedbackHistoryPage);
              }
            ],
          ),
          SliverPadding(
            padding: EdgeInsets.all(20.w),
            sliver: SliverList(
              delegate: SliverChildListDelegate([
                // 反馈类型选择
                _buildFeedbackTypeSelector(),
                SizedBox(height: 30.h),

                // 反馈内容输入
                _buildFeedbackInput(),
                SizedBox(height: 20.h),

                // 匿名选项
                _buildAnonymousOption(),
                SizedBox(height: 30.h),

                // 提交按钮
                _buildSubmitButton(),
              ]),
            ),
          ),
        ],
      ),
    );
  }

  // 反馈类型选择器
  Widget _buildFeedbackTypeSelector() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('反馈类型', style: TextStyle(fontSize: 32.sp, fontWeight: FontWeight.bold)),
        SizedBox(height: 15.h),
        Obx(() => Wrap(
          spacing: 15.w,
          runSpacing: 15.h,
          children: List.generate(feedbackTypes.length, (index) {
            final type = feedbackTypes[index];
            return ChoiceChip(
              label: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(type['icon'], size: 20.w),
                  SizedBox(width: 5.w),
                  Text(type['title']),
                ],
              ),
              selected: _selectedFeedbackType.value == index,
              onSelected: (selected) {
                if (selected) _selectedFeedbackType.value = index;
              },
              backgroundColor: Colors.white,
              selectedColor: Colors.blue[100],
              labelStyle: TextStyle(
                color: _selectedFeedbackType.value == index
                    ? Colors.blue
                    : Colors.grey[700],
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20.r),
                side: BorderSide(
                  color: _selectedFeedbackType.value == index
                      ? Colors.blue
                      : Colors.red,
                ),
              ),
            );
          }),
        )),
      ],
    );
  }

  // 反馈内容输入框
  Widget _buildFeedbackInput() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('反馈内容', style: TextStyle(fontSize: 32.sp, fontWeight: FontWeight.bold)),
        SizedBox(height: 15.h),
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12.r),
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 5,
                offset: Offset(0, 2),
              ),
            ],
          ),
          child: TextField(
            controller: _feedbackController,
            maxLines: 8,
            maxLength: 500,
            decoration: InputDecoration(
              hintText: '请详细描述您的问题或建议...',
              border: InputBorder.none,
              contentPadding: EdgeInsets.all(15.w),
              counterText: '',
            ),
          ),
        ),
        Align(
          alignment: Alignment.centerRight,
          child: Text(
            '${_feedbackController.text.length}/500',
            style: TextStyle(color: Colors.grey),
          ),
        )
      ],
    );
  }

  // 匿名选项
  Widget _buildAnonymousOption() {
    return Obx(() => Row(
      children: [
        Checkbox(
          value: _isAnonymous.value,
          onChanged: (value) => _isAnonymous.value = value ?? false,
          activeColor: Colors.blue,
        ),
        Text('匿名反馈', style: TextStyle(fontSize: 30.sp)),
      ],
    ));
  }

  // 提交按钮
  Widget _buildSubmitButton() {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        onPressed: _submitFeedback,
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.blue,
          padding: EdgeInsets.symmetric(vertical: 30.h),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30.r),
          ),
          elevation: 3,
        ),
        child: Text(
          '提交反馈',
          style: TextStyle(
            fontSize: 26.sp,
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

  // 提交反馈方法
  void _submitFeedback() {
    if (_feedbackController.text.trim().isEmpty) {
      Get.snackbar('提示', '请填写反馈内容');
      return;
    }

    final feedbackData = {
      'type': feedbackTypes[_selectedFeedbackType.value]['title'],
      'content': _feedbackController.text,
      'isAnonymous': _isAnonymous.value,
      'timestamp': DateTime.now().toIso8601String(),
    };

    // 这里调用逻辑层的提交方法
    // controller.submitFeedback(feedbackData);

    Get.back();
    Get.snackbar('成功', '感谢您的反馈！');
  }

  @override
  void dispose() {
    _feedbackController.dispose();
    _selectedFeedbackType.close();
    _isAnonymous.close();
  }
}
